<?php
/*
 *	产品接口模块
 */
namespace app\api\controller\app;

use think\facade\Db;
use app\common\model\ClothMain as C_M;
use app\common\model\ClothColor as COLOR_M;

class Cloth extends Base{
	// 默认筛选条件
    private $field='id,title,number,kgm,breadth,side_type,square,component,tube,emptykg,otube,oemptykg,rebate_kg,thumb,cover,content';
    
	public function initialize(){
		parent::initialize();
	}
	
	public function index(){
		$data=$this->getClothCache('table');
		return $this->p_list($data);
	}
	
	public function find(){
		$keyword=I('keyword');
		if($keyword!=''){
			$data=C_M::getList([
				['uuid','=',$this->uuid],
				['wx_isshow','=',1]
			],'',$this->field);
		}
		return $this->p_list($data['data'],$data['extend']);	
	}
	
	//产品详情
	public function info(){
		$id=I('id');
		$data=$this->getClothCache('color',$id);
		if(!$data){
			return $this->p_error('该面料不存在！');
		}else{
			//页面分享参数
			$this->share=[
				'title'=>'海帆布业 - '.$data['t']['title'].'(电子色卡)',
				//'imageUrl'=>$this->host_images($data['t']['thumb']),	//阿里主图会分享无效
				'imageUrl'=>$this->host_images('/static/images/xcx_share.jpg'),
				'path'=>'/pages/cloth/info?id='.$data['t']['id'],
			];
			return $this->p_list($data,['share'=>$this->share]);	
		}
	}
	
	//潘通色卡查询
	public function pangtong(){
		$ptid=I('ptid');
		$pattern='/^#([a-fA-F\d]{6}|[a-fA-F\d]{3})$/';
		preg_match($pattern,$ptid,$match);
		if(count($match)>0){		//查询电子色
			$rgb=hex2rgba($ptid);
			$hex=$ptid;
			$data=[
				'ptHex'=>$hex,
				'ptRgb'=>$rgb,
				'ptCode'=>'无',
			];
		}else{				//查询潘通色卡
			$url='https://www.qtccolor.com/findcolor.aspx';
			$get=[
				'ajax'=>1,
				'word'=>$ptid,
			];
			$get_data=curlGet($url,$get,4);
			if(count($get_data)>0){
				$data=[
					'ptHex'=>$get_data[0]['ColorlibraryItems'][0]['ColorValue_Hex'],
					'ptRgb'=>$get_data[0]['ColorlibraryItems'][0]['ColorValue'],
					'ptCode'=>$get_data[0]['ColorlibraryItems'][0]['ColorCode'],
				];
				$hex=$data['ptHex'];
			}else{
				return $this->p_error(102);
			}
		}
		$data['list']=$this->analogyColor($hex,10,'',15);
		if(count($data['list'])<=0){
			return $this->p_error(102);
		}else{
			return $this->p_list($data);
		}
	}
	
	//读取产品缓存
	protected function getClothCache($type,$id=0,$ext=''){
		$put_data=array();

		if($type=='table'){							//读取产品列表 
			$class=F($this->uuid.'/minApp/colth_class');

			if($class==''){
				$res_list=C_M::listData(['status'=>1,'wx_isshow'=>1],'sort asc,square',$this->field);
				$class=get_type_array($res_list,'tid','tid,type','',true);
				//array_unshift($class,array('tid'=>0,'type'=>'全部'));

				F($this->uuid.'/minApp/colth_class',$class);
			}
			
			$put_data = $class;


		}else if($type=='color'){					//读取产品的颜色列表

			$color=F($this->uuid.'/minApp/color_'.$id);
			if($color==''){

				$T=C_M::info($id);
				if($T!=''){
					$C=COLOR_M::listData(['pid'=>$id,'is_show'=>1,'gray'=>0],'sort asc,colorid asc');
					//初始化产品图片
					if($T['thumb']==''){
						$T['thumb']=$this->host_images('/static/images/nopic.jpg');
					}else{
						$T['thumb']=$this->host_images($T['thumb']);
					}

					if($T['seka']!=''){
						$T['seka']=$this->replace_img($T['seka']);
					}
					
					$T['cover']=$this->cover_img($T['cover']);
					
					$put_data=array('color'=>$C,'t'=>$T,'id'=>$id);
					F($this->uuid.'/minApp/color_'.$id,$put_data);
				}else{
					return false;
				}
			}else{
				$put_data=$color;
			}
			
		}

		return $put_data;
	}

	//净布价取整
	protected function r_integer($val){
		$val=round($val,1);
		$min_int=explode('.',$val);
		if($min_int[1]<5){
			$val=$min_int[0]+0.5;
		}else{
			$val=$min_int[0]+1;
		}
		return $val;
	}
	
	// 图路径替换
	protected function replace_img($val){
		$val=str_replace('/static/images',get_api_host().'/static/images',$val);
		return $val;
	}
	
	//替换主图封面
	protected function cover_img($data){
		if($data!=""){
			foreach($data as $k=>$v){
				$data[$k]=$this->host_images($v);
			}
		}
		return $data;
	}
}
?>